var Tong = (function (self) {
    self.Version = '1.0.0';
    var _hasTouchEvent = 'ontouchstart' in window;
    //根据UserAgent判断访问网站的平台
    var _agent = navigator.userAgent.toLowerCase();
    var _chars16 = '0123456789ABCDEF'.split('');
    /**
     * 标识当前UserAgent的字典变量
     * @type {Object}
     */
    window.mAgent = {
        other: 0,
        ios: 1,
        android: 2,
        windows: 3
    };
    /**
     * 标识承载当前页面的UserAgent的APP种别
     * @type {Object}
     */
    window.mApp = {
        none: 0,
        tujia: 1,
        weixin: 2,
        gaode: 3,
        qqbrowser: 4,
        ucbrowser: 5,
        hmbrowser: 6,
        baidubrowser: 7,
        safaribrowser: 8,
        _360browser: 9,
        operabrowser: 10,
        chromebrowser: 11,
        alipay: 12
    };

    var _mAgent = mAgent.other;

    if (_agent.indexOf('android') > -1)
    {
        _mAgent = mAgent.android;
    } else if (_agent.indexOf('iphone') > -1 || _agent.indexOf('ipod') > -1 || _agent.indexOf('ipad') > -1)
    {
        _mAgent = mAgent.ios;
    } else if (_agent.indexOf('windows') > -1)
    {
        _mAgent = mAgent.windows;
    }
    var _mApp = mApp.none;
    if (_agent.indexOf('micromessenger') > -1)
    {
        _mApp = mApp.weixin;
    } else if (_agent.indexOf('alipay') > -1) {
        _mApp = mApp.alipay;
    } else if (_agent.indexOf('mqqbrowser') > -1)
    {
        _mApp = mApp.qqbrowser;
    } else if (_agent.indexOf('ucbrowser') > -1)
    {
        _mApp = mApp.ucbrowser;
    } else if (_agent.indexOf('miuibrowser') > -1)
    {
        if (_agent.indexOf('build/hm') > -1)
        {
            _mApp = mApp.hmbrowser;
        }
    } else if (_agent.indexOf('baidubrowser') > -1)
    {
        _mApp = mApp.baidubrowser;
    } else if (_agent.indexOf('safari') > -1)
    {
        _mApp = mApp.safaribrowser;
    } else if (_agent.indexOf('opera') > -1)
    {
        _mApp = mApp.operabrowser;
    } else if (_agent.indexOf('chrome') > -1)
    {
        _mApp = mApp.chromebrowser;
    } else if (_agent.indexOf('360') > -1)
    {
        _mApp = mApp._360browser;
    }

    /**
     * 数据类型字典变量
     * @type {Object}
     */
    window.type = {
        eUndefined: 'undefined',
        eNull: 'null',
        eNumber: 'number',
        eBoolean: 'boolean',
        eString: 'string',
        eFunction: 'function',
        eRegExp: 'regexp',
        eArray: 'array',
        eDate: 'date',
        eError: 'error',
        eNode: 'node',
        eElement: 'element',
        eDocument: 'document',
        eArraylist: 'arraylist',
        eObject: 'object'
    };
    var class2type = {
        'undefined': type.eUndefined,
        'number': type.eNumber,
        'boolean': type.eBoolean,
        'string': type.eString,
        'function': type.eFunction,
        '[object Boolean]': type.eBoolean,
        '[object Number]': type.eNumber,
        '[object String]': type.eString,
        '[object Function]': type.eFunction,
        '[object RegExp]': type.eRegExp,
        '[object Array]': type.eArray,
        '[object Date]': type.eDate,
        '[object Error]': type.eError
    };
    var core_toString = class2type.toString;

    /**
     *  获取包装对象
     * @param {type} parameter
     * @returns {Tong_L1.TongClass}
     */
    function __fetcher(parameter) {
        var list = [];
        return new TongClass(list);
    }
    /**
     * 数组转换为字典
     * @param {type} arr
     * @returns {Tong_L1.array2Dictionary}
     */
    function array2Dictionary(arr)
    {
        this.length = arr.length;
        for (var i = 0; i < arr.length; ++i)
        {
            this[i] = arr[i];
        }
    }

    function TongClass(objArray) {
        this.innerObjectList = objArray;
        array2Dictionary.call(this, this.innerObjectList);
    }

    TongClass.prototype = self;

    //Global method begin
    /**
     * 获取当前UserAgent的标识码
     * @return {*}
     */
    __fetcher.agent = function () {
        return _mAgent;
    };


    /**
     * 获取当前内嵌的应用标识码
     * @return {*}
     */
    __fetcher.app = function () {
        return _mApp;
    };

    /**
     * 判断对象是否为空
     * @param obj
     * @return {Boolean}
     */
    __fetcher.isObjectNull = function (obj)
    {
        if (obj === undefined)
        {
            return true;
        }
        if (!__fetcher.isObject(obj))
        {
            throw 'Parameter is not an object!';
        }
        return obj === null;
    };

    /**
     * 判断一个变量是否未定义或者为空
     * @param v
     */
    __fetcher.isNullOrUndefined = function (v) {
        if (v === undefined)
        {
            return true;
        }
        return v === null;
    };
    /**
     * 判断字符串是否为空
     * @param str
     * @return {Boolean}
     */
    __fetcher.isStringEmpty = function (str)
    {
        var ty = __fetcher.checkType(str);
        if (ty === type.eUndefined || ty === type.eNull)
        {
            return true;
        }
        if (ty !== type.eString)
        {
            throw 'Parameter is not a string!';
        }
        if (__fetcher.isObject(str))
        {
            return str.valueOf() === '';
        } else
        {
            return str === '';
        }
    };
    /**
     * 判断参数是否为对象
     * @param obj
     * @return {Boolean}
     */
    __fetcher.isObject = function (obj)
    {
        return 'undefined,number,boolean,string'.indexOf(typeof obj) < 0;
    };
    /**
     * 判断参数的类型
     * @param obj
     * @return {window.type}
     */
    __fetcher.checkType = function (obj)
    {
        var ty = typeof obj;
        if (class2type[ty])
        {
            return class2type[ty];
        }
        if (obj == null)
        {
            return type.eNull;
        }
        ty = core_toString.call(obj);
        if (class2type[ty])
        {
            return class2type[ty];
        } else if (obj instanceof Element)
        {
            return type.eElement;
        } else if (obj instanceof Document)
        {
            return type.eDocument;
        } else if (obj instanceof Node)
        {
            return type.eNode;
        } else if (isArrayList(obj))
        {
            return type.eArraylist;
        } else
        {
            return type.eObject;
        }
    };

    /**
     * 加载Url
     * @param url
     */
    __fetcher.loadPage = function (url)
    {
        window.location.href = url;
    };

    /**
     * 判断是否支持触屏事件
     * @return {Boolean}
     */
    __fetcher.hasTouchEvent = function () {
        return _hasTouchEvent;
    };

    /**
     * 生成GUID
     * @return {String}
     */
    __fetcher.guid = function () {
        var uuid = [], i;
        // rfc4122, version 4 form
        var r;
        // rfc4122 requires these characters
        uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
        uuid[14] = '4';
        // Fill in random data.  At i==19 set the high bits of clock sequence as
        // per rfc4122, sec. 4.1.5
        for (i = 0; i < 36; i++)
        {
            if (!uuid[i])
            {
                r = 0 | Math.random() * 16;
                uuid[i] = _chars16[(i == 19) ? (r & 0x3) | 0x8 : r];
            }
        }
        return uuid.join('');
    };
    //对外部开发接口
    window.mtong = __fetcher;

    function isArrayList(obj)
    {
        /* Real arrays are array-like
         if (obj instanceof Array)
         {
         return true;
         }*/
        // Arrays must have a length property
        if (!('length' in obj))
        {
            return false;
        }
        // Length must be a number
        if (typeof obj.length != 'number')
        {
            return false;
        }
        // and nonnegative
        if (obj.length < 0)
        {
            return false;
        }
        if (obj.length > 0)
        {
            // If the array is nonempty, it must at a minimum
            // have a property defined whose name is the number length-1
            if (!((obj.length - 1) in obj))
            {
                return false;
            }
        }
        return true;
    }
    ;

    return self;
})(Tong || {});

